﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace Phong_CTSV
{
    public class Collection
    {
        public Collection() { }
        private static Data_Table[,] getDataTable(long uni,int numRow,int numColumn,int formID)
        {
            Data_Table[,]dataTable = new Data_Table[numRow, numColumn];
            QuerrySQL query = new QuerrySQL();
            for (int i = 0; i < numRow; i++)
            {
                for (int j = 0; j < numColumn; j++)
                {
                    dataTable[i, j] = query.Select_Data_Cell(formID, uni, j, i, System.DateTime.Today.Year);
                }
            }
            return dataTable;
        }
        static void ResetDataTable(ref Data_Table[,] table, int numRow, int numCol, List<Columns_Header> paraColHeader)
        {
            for (int i = 0; i < numRow; i++)
                for (int j = 0; j < numCol; j++)
                {
                    table[i, j] = new Data_Table();
                    table[i, j].Value_type = paraColHeader[j].Kind;
                    if (table[i, j].Value_type.Equals("int"))
                        table[i, j].Value = "0";
                    else
                        table[i, j].Value = "";
                }
        }
        public static Data_Table[,] Collection_Value(string formName, int year)
        {
            QuerrySQL _querry = new QuerrySQL();
            List<Columns_Header> col_header = new List<Columns_Header>();
            List<Columns_Header> col_Temp = _querry.Select_ColumnsHeaderByForm(formName);
            foreach (Columns_Header header in col_Temp)
                if (!header.Has_child)
                    col_header.Add(header);
            List<Rows_Header> row_header = _querry.Select_RowsHeaderByForm(formName);
            List<List<Data_Table>> data_temp = new List<List<Data_Table>>(); // chua bang tam luc tinh tung cot
            List<Data_Table> temp = new List<Data_Table>(); // chua gia tri cua tung cot de add vao bang tam
            int numberRow = row_header.Count;
            int numberCol= col_header.Count-1;
            Form form = _querry.Select_Form(formName);
            int formID = (int)form.Id;
            int uniID = (int)_querry.Select_AllUniversity()[0].ID;
            Data_Table[,] dataValue = getDataTable(uniID,numberRow,numberCol,(int)form.Id);
            int runCol = 0;
            int runRow = 0;
            int col_number = col_header.Count;
            ResetDataTable(ref dataValue,numberRow,numberCol,col_header);
            if(form.Has_rows)
                col_number -=1;
            for (int j = 0;j <col_number;j++)
            {
                
                List<University> uniList = _querry.Select_AllUniversity();
                foreach (University uni in uniList)
                {
                    temp = _querry.Select_Data_Columns(formID, (int)uni.ID, j, year);
                    data_temp.Add(temp);
                }
                foreach (List<Data_Table> dt in data_temp)
                {
                    runRow = 0;
                    foreach (Data_Table table in dt)
                    {
                        if (dataValue[runRow, runCol].Value_type.Equals("int"))
                        {
                            
                            try
                            {
                                long value1 = Int64.Parse(dataValue[runRow, runCol].Value);
                                long value2 = Int64.Parse(table.Value);
                                dataValue[runRow, runCol].Value = (value1 + value2).ToString();
                            }
                            catch {}
                       
                        }
                        else
                        {
                            dataValue[runRow, runCol].Value = dataValue[runRow, runCol].Value + "\n" + table.Value;
                        }
                        runRow++;
                    }
                }
                runCol++;
            }
            return dataValue;
        }
        
    }
}